﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;
using System.Linq;
using Besture.Bll.UserBase;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;

namespace Besture.UI
{
    public partial class OrderSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            try
            { 
                //页面加载
                InitPage();
                //编辑编号
                InitData(ID); 
            }
            catch
            {
                // ignored
            }
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            InitPage();
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        { 

            var dac = new OrderLogDac();
            grid.DataSource = dac.GetDataTableByOrderyID(ID);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new OrderDac();
            var model = dac.GetModel(id);
			if(model==null) return; 

            //旅客信息 绑定  
            rpPassengerOrder.DataSource = new PassengerOrderDac().GetDataTableByOrderId(id);
            rpPassengerOrder.DataBind();

            //隐藏保存按钮 
            if (model.OrderStatus == 2) Button2.Visible = false;//2 表示 取消确认
            if (model.OrderStatus >= 4) Button2.Visible = false;//4标示 已付款

            lblOrderNo.Text = model.OrderNo;

            lblLatestTime.Text = Convert.ToDateTime(model.LatestTime).ToString("yyyy-MM-dd");
            lblContactName.Text = model.ContactName;
            lblContactPhone.Text = model.ContactPhone;
            lblContactEmail.Text = model.ContactEmail;
            lblOtherClaims.Text = model.OtherClaims;
            lblOtherClaimsRemark.Text = model.OtherClaimsRemark;
            lblCopePrice.Text = string.Format("RMB {0}", model.CopePrice);
            lblDeductiblePrice.Text = string.Format("RMB {0}",model.DeductiblePrice);
            lblNightNumber.Text = string.Format("{0} 夜间数抵扣 <b>RMB {1}</b>", model.NightNumber, model.NightNumberPrice); //"100夜间数抵扣 <b>RMB100.00</b>";
            txtPreferentialPrice.Text = model.PreferentialPrice.ToString();
            lblCopePrice1.Text = string.Format("<b>RMB {0}</b>", model.CopePrice);
            lblActuallyPaidPrice.Text = string.Format("<b>RMB {0}</b>", model.ActuallyPaidPrice);
            lblPayTime.Text = Convert.ToDateTime(model.PayTime).ToString("yyyy-MM-dd");


            lblUserID.Text = new UserBaseDac().GetModel(model.UserID) == null ? "" : new UserBaseDac().GetModel(model.UserID).NickName;
            lblHotelsID.Text = new HotelsDac().GetModel(model.HotelsID) == null ? "" : new HotelsDac().GetModel(model.HotelsID).HotelsName;
            lblRoomID.Text = new RoomDac().GetModel(model.RoomID) == null ? "" : new RoomDac().GetModel(model.RoomID).RoomName;
            

            if (!string.IsNullOrEmpty(model.RoomID.ToString()))
            {
                string sName = string.Empty;
                string sCode = new RoomDac().GetModel(model.RoomID).RoomFacilities;
                var v = QueryDac.GetTable(string.Format("select * from TBDictionaryType where Type=8 and Code in({0})",sCode));
                sName = v.Rows.Cast<DataRow>().Aggregate(sName, (current, dr) => current + (dr["Name"] + ","));
                lblRoomFacilities.Text = sName.TrimEnd(',');
            } 
            lblOrderStatus.Text = new DictionaryTypeDac().GetDataTableByTypeAndCode(7, model.OrderStatus) != null
                ? "<span style='display: none;'>" + model.OrderStatus + "</span>" +
                  new DictionaryTypeDac().GetDataTableByTypeAndCode(7, model.OrderStatus).Name
                : "<span style='display: none;'>" + model.OrderStatus + "</span>0";
            
            //2015-4-13 至 2015-4-14 共 1 晚
            lblDepartureDate.Text = string.Format("{0} 至 {1} 共 {2} 晚",
                Convert.ToDateTime(model.CheckDate).ToString("yyyy-MM-dd"),
                Convert.ToDateTime(model.DepartureDate).ToString("yyyy-MM-dd"),
               DateDiff(Convert.ToDateTime(model.DepartureDate), Convert.ToDateTime(model.CheckDate)));

            txtReservationNumber.Text = model.ReservationNumber ?? ""; //酒店预订号
        }

        /// <summary>
        /// 计算天数
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        private static int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateStart.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            return sp.Days;
        }

        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new OrderDac();
            OrderModel model = dac.GetModel(ID); 
            if (model.OrderStatus==1)
                model.OrderStatus = model.OrderStatus + 2;
            else
              model.OrderStatus = model.OrderStatus + 1;


            //201507291530 lyg 添加log日志 start
            string sLogContent = string.Format("{0}已在{1}已预约确认订单(订单号：{2})", AdminUserService.AdminLoginUser.LoginName, DateTime.Now.ToString(),
                model.OrderNo);
            new OrderLogDac().Add(sLogContent, ID);
            //201507291530 lyg 添加log日志 end


            if (dac.UpdateByModel(model) > 0)
                FunHelper.Alert("保存成功", "/admin/Order/OrderSet.aspx?id="+ID);

        }


        #endregion
         
        #region  修改优惠价 酒店预订号

        /// <summary>
        /// 修改优惠价 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnPreferentialPriceClick(object sender, EventArgs e)
        {
            var dac = new OrderDac();
            OrderModel model = dac.GetModel(ID);
            model.PreferentialPrice = Convert.ToDecimal(txtPreferentialPrice.Text);

            //201507291530 lyg 添加log日志 start
            string sLogContent = string.Format("{0}已在{1}修改订单【{2}】的优惠价【{3}】", AdminUserService.AdminLoginUser.LoginName, DateTime.Now.ToString(),
                model.OrderNo, model.PreferentialPrice);
            new OrderLogDac().Add(sLogContent, ID);
            //201507291530 lyg 添加log日志 end


            if (dac.UpdateByModel(model) > 0)
            {
                FunHelper.Alert("优惠价修改成功", "/admin/Order/OrderSet.aspx?id=" + ID);
            } 
        }

        /// <summary>
        /// 修改 酒店预订号 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnReservationNumberClick(object sender, EventArgs e)
        {
            var dac = new OrderDac();
            OrderModel model = dac.GetModel(ID);
            model.ReservationNumber = txtReservationNumber.Text;

            //201507291530 lyg 添加log日志 start
            string sLogContent = string.Format("{0}已在{1}修改订单【{2}】的酒店预订号【{3}】", AdminUserService.AdminLoginUser.LoginName, DateTime.Now.ToString(),
                model.OrderNo, model.ReservationNumber);
            new OrderLogDac().Add(sLogContent, ID);
            //201507291530 lyg 添加log日志 end

            if (dac.UpdateByModel(model) > 0)
            {
                FunHelper.Alert("预订号修改成功", "/admin/Order/OrderSet.aspx?id=" + ID);
            }
        }

        
        #endregion

        protected void btnClose_Click(object sender, EventArgs e)
        {
            var dac = new OrderDac();
            OrderModel model = dac.GetModel(ID);
            model.OrderStatus =2;

            //201507291530 lyg 添加log日志 start
            string sLogContent = string.Format("{0}已在{1}将订单【{2}】的取消", AdminUserService.AdminLoginUser.LoginName, DateTime.Now.ToString(),
                model.OrderNo);
            new OrderLogDac().Add(sLogContent, ID);
            //201507291530 lyg 添加log日志 end

            if (dac.UpdateByModel(model) > 0)
            {
                FunHelper.Alert("取消成功", "/admin/Order/OrderSet.aspx?id=" + ID);
                Button2.Visible = false;
            }
        }


    }
}